<?php
/*
    Copyright 2010 Justin Lipton, Jonathan Rosenberg
    
    This file is part of MediaList.

    MediaList is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MediaList is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MediaList.  If not, see <http://www.gnu.org/licenses/>.
*/

$user=$_GET["user"];
$showid=$_GET["showid"];
$prev=$_GET["prev"];

$con = mysql_connect("localhost", "root");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("DB", $con);

$sql="SELECT * FROM ".$user." WHERE ShowId='".$showid."'";
$result = mysql_query($sql, $con);
$row = mysql_fetch_array($result);

$sql="SELECT * FROM Shows WHERE Showname='".$row["ShowName"]."'";
$result = mysql_query($sql, $con);
$row2 = mysql_fetch_array($result);

$sql="SELECT * FROM Users WHERE Username='".$user."'";
$result=mysql_query($sql, $con);
$row4=mysql_fetch_array($result);

$sql="UPDATE ".$user." SET Progress='".$row2['NumEpisodes']."' WHERE ShowId='".$showid."'";
mysql_query($sql, $con);

if($prev=='W')
{
    $sql="UPDATE Users SET NumCompleted='".($row4["NumCompleted"]+1)."'
    , NumViewing='".($row4["NumViewing"]-1)."' WHERE Username='".$user."'";
    mysql_query($sql, $con);
}
else if($prev=='P')
{
    $sql="UPDATE Users SET NumCompleted='".($row4["NumCompleted"]+1)."'
    , NumPlanning='".($row4["NumPlanning"]-1)."' WHERE Username='".$user."'";
    mysql_query($sql, $con);
}
else if($prev=='D')
{
    $sql="UPDATE Users SET NumCompleted='".($row4["NumCompleted"]+1)."'
    , NumDropped='".($row4["NumDropped"]-1)."' WHERE Username='".$user."'";
    mysql_query($sql, $con);
}
else if($prev=='N')
{
    $sql="UPDATE Users SET NumCompleted='".($row4["NumCompleted"]+1)."' WHERE Username='".$user."'";
    mysql_query($sql, $con);
}
if($prev=='C')
{
    //special case for compatibiliy stuff, shouldn't ever enter here
}
else{

    $sql="UPDATE ".$user." SET Status='C' WHERE ShowId='".$showid."'";
    mysql_query($sql, $con);

    $sql = "UPDATE Shows SET TotalScores='".($row2["TotalScores"]+$row["Rating"])."', 
    TotalSeen='".($row2["TotalSeen"]+1)."' WHERE Showname='".$row["ShowName"]."'";
    mysql_query($sql, $con);
    
    if($row["Rating"]>0)
    {
        $sql="INSERT INTO P".str_replace(' ','_',$row["ShowName"])." (UserName, Rating) 
            VALUES ('".$row4["Username"]."', '".$row["Rating"]."')";
        mysql_query($sql, $con); //update the show completed list with User and rating
    
        $sql="SELECT * FROM P".str_replace(' ','_',$row["ShowName"])." WHERE Username<>'".$user."'";
        $result3 = mysql_query($sql, $con);
        while($row5 = mysql_fetch_array($result3))
        //while($stmt->fetch())
        {
            //update the compatibility score to +1 if +-1
            if(($row5["Rating"]-$row["Rating"])>=-1 && ($row5["Rating"]-$row["Rating"])<=1)
            {
                $sql = "SELECT Score FROM comp".$row5["Username"]." WHERE Username='".$user."'";
                $result2=mysql_query($sql, $con);
                $row3=mysql_fetch_array($result2);
                $sql = "UPDATE comp".$user." SET Score='".($row3["Score"]+1)."' 
                    WHERE Username='".$row5["Username"]."'";
                mysql_query($sql, $con);
                $sql = "UPDATE comp".$row5["Username"]." SET Score='".($row3["Score"]+1)."'
                    WHERE Username='".$user."'";
                mysql_query($sql, $con);
            }
            //update the compatibility score to -1 if +-4 or more                
            else if(($row5["Rating"]-$row["Rating"])<=-4 || ($row5["Rating"]-$row["Rating"])>=4)
            {
                $sql = "SELECT Score FROM comp".$row5["Username"]." WHERE Username='".$user."'";
                $result2=mysql_query($sql, $con);
                $row3=mysql_fetch_array($result2);
                $sql = "UPDATE comp".$user." SET Score='".($row3["Score"]-1)."' 
                    WHERE Username='".$row5["Username"]."'";
                mysql_query($sql, $con);
                $sql = "UPDATE comp".$row5["Username"]." SET Score='".($row3["Score"]-1)."'
                    WHERE Username='".$user."'";
                mysql_query($sql, $con);
            }
            //otherwise don't update compatibility(effective +0)
        }
    }
    else
    {
        $sql="INSERT INTO P".str_replace(' ','_',$row["ShowName"])." (UserName, Rating) 
              VALUES ('".$row4["Username"]."', NULL)";
        mysql_query($sql, $con); //update the show completed list with User and rating
    }
}
echo "<meta http-equiv=\"REFRESH\" content=\"0;url=table.php?currentUser=".$user."\">";

mysql_close($con);
?>